﻿using System;
using System.Collections.Generic;
using System.Linq;
using Core.DAL;
using Core.Extentions;
using Core.Utils;

namespace Core.BOL
{
    public class NewsRepository
    {
        private ScoutNetDataContext context = null;

        #region Construtor
        public NewsRepository()
        {
            context = GeneralUtils.GetDataContext();
        }
        #endregion

        #region Methodes
        public void InsertNews(tSite_New news)
        {   
            context.tSite_News.InsertOnSubmit(news);
        }

        public List<tSite_New> GetLatestNews(int numOfDays,int skip, int take)
        {
            var query = (from n in context.tSite_News
                         join s in context.tSite_Sections on n.SID equals s.ID
                         where n.InsertDate >= DateTime.Now.GetNowTimeByTimeZone().AddDays(- numOfDays)
                         orderby n.InsertDate descending
                         select n);

            if (take > -1)
            {
                return query.Skip(skip).Take(take).ToList();
            }

            return query.ToList();
        }

        public List<tSite_New> GetAllNews()
        {
            return GetNews(0,-1); 
        }

        public List<tSite_New> GetNews(int skip, int take)
        {
            var query = (from n in context.tSite_News
                         join s in context.tSite_Sections on n.SID equals s.ID
                         orderby n.InsertDate descending
                         select n);

            if (take > -1)
            {
                return query.Skip(skip).Take(take).ToList();
            }
            
            return query.ToList();
            
        }

        public List<tSite_New> GetNewsBySectionName(string sectionName, int skip, int take)
        {
            var query = (from n in context.tSite_News
                         join s in context.tSite_Sections on n.SID equals s.ID
                         orderby n.InsertDate
                         where s.SystemName == sectionName
                         orderby n.InsertDate descending
                         select n).Skip(skip).Take(take).ToList();
            return query;
        }

        public tSite_New GetNewsByID(int ID)
        {
            var query = (from n in context.tSite_News
                         join s in context.tSite_Sections on n.SID equals s.ID
                         where n.ID == ID
                         orderby n.InsertDate ascending
                         select n).Take(1).SingleOrDefault();
            return query;
        }

        public Int32 GetCountNews(string systemName)
        {
            Int32 query = (from n in context.tSite_News
                           join s in context.tSite_Sections on n.SID equals s.ID
                           where s.SystemName == systemName
                           select n).Count();

            return query;
        }

        public List<tSite_New> GetPreviousNews(DateTime insertDate, int take, int sectionID)
        {
            var query = (from n in context.tSite_News
                         join s in context.tSite_Sections on n.SID equals s.ID
                         where n.InsertDate < insertDate && n.SID == sectionID
                         orderby n.InsertDate descending
                         select n).Take(take).ToList();
            return query;
        }

        public List<tSite_New> GetNextNews(DateTime insertDate, int take, int sectionID)
        {
            var query = (from n in context.tSite_News
                         join s in context.tSite_Sections on n.SID equals s.ID
                         where n.InsertDate > insertDate && n.SID == sectionID
                         orderby n.InsertDate ascending
                         select n).Take(take).ToList();
            return query;
        }

        public void DeleteNews(int id)
        {
            tSite_Photos_Album tAlbum = context.tSite_Photos_Albums.SingleOrDefault(p => p.NID == id);
            if(tAlbum != null)
            {
                tAlbum.NID = null;
            }

            tSite_Convoc tConvoc = context.tSite_Convocs.SingleOrDefault(p => p.NID == id);
            if (tConvoc != null)
            {
                tConvoc.NID = null;
            }

            tSite_New tNews = context.tSite_News.SingleOrDefault(p => p.ID == id);
            if (tNews != null)
            {
                context.tSite_News.DeleteOnSubmit(tNews);
            }
        }

        public void Save()
        {
            context.SubmitChanges();
        }
        #endregion
    }
    
}
